package com.deneme;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

import functions.images.CommonHTML;
import functions.images.CommonHTML.PageName;

public class Index extends HttpServlet {
    private static BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();

	public void doGet(HttpServletRequest req,
			HttpServletResponse resp)
					throws IOException {
		/* get the current user's information */
		UserService userService = UserServiceFactory.getUserService();
		User user = userService.getCurrentUser();
		/* strings to contain content for the page */
		String content = "";
		String formData = "";
		String linkOptions = "";
		/* if the user is not logged in, set content to be the login button */
		if (user == null) {
			content = "<a class='login' href=\"" + userService.createLoginURL("/") +"\">Login</a>" +
					"<div class='logo'><img src='/images/cloud_logo.png' /></div>";
		} else {
		/* if the user is logged in, show the regular home page */
			UserData userData = UserData.getDataForUser(user);
			if (userData != null) {
				deleteImages(userData); // deletes the small temporary images for puzzles they have played 
				/* form to upload a file from the computer */
				formData = "<div class='upload_form'>" +
							"Upload pictures from your computer: <br /><br />\n" +
							"<form action='" + blobstoreService.createUploadUrl("/upload") +"' method='post' enctype='multipart/form-data'>\n" + 
							"<input type='file' name='myFile'>\n" +
							"<input type='submit' value='Submit'>\n" +
							"</form>" +
							"</div><!-- upload_form -->\n";
				content = CommonHTML.getNavBar(PageName.upload) + // includes the menu bar
						"<br /><br />" + 
						formData +
						"<br />" +
						"<div class='logo'><img src='/images/cloud_logo.png' /></div>";
			}
		}
		
		/* create the actual page data, and add content to it */
		resp.setContentType("text/html");
		PrintWriter out = resp.getWriter();
		out.print("<html>\n" +
				"<head>\n" +
				"	<link rel=\"stylesheet\" href=\"/styles.css\" type=\"text/css\" />\n" +
				"	<title>Welcome to n-puzzle!</title>\n" +
				"</head>\n" +
				"<body>\n" +
				"	<div class='content'>" +
				content +
				"	</div><!-- content -->" +
				"</body>\n" +
				"</html>\n");
	}
	
	/* this function deletes small images so they don't clog up the blobstore */
	private void deleteImages(UserData data) {
		ArrayList<BlobKey> toDelete = data.getImagesToDelete();
		if(toDelete != null) {
			for(BlobKey key : toDelete) {
	    		blobstoreService.delete(key);
			}
			data.setImagesToDelete(null);
		}
	}

}
